import { configureStore } from '@reduxjs/toolkit'
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'

import scriptReducer from './slices/scriptSlice'
import connectionReducer from './slices/connectionSlice'
import executionReducer from './slices/executionSlice'
import versionReducer from './slices/versionSlice'
import uiReducer from './slices/uiSlice'

export const store = configureStore({
  reducer: {
    script: scriptReducer,
    connection: connectionReducer,
    execution: executionReducer,
    version: versionReducer,
    ui: uiReducer,
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: ['persist/PERSIST'],
      },
    }),
})

export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch

// 类型化的hooks
export const useAppDispatch = () => useDispatch<AppDispatch>()
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector